﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
//using ACCDatasetTableAdapters;
using System.Collections;
using System.Web.UI;
using System.Configuration;
/// <summary>
/// Summary description for Menus
/// </summary>
public class Menus
{
    public Menus()
    {
        //
        // TODO: Add constructor logic here
        //
        //Check for cache 
    }


    //public static DataSet MenuDataSet()
    //{
    //    MenuTableAdapter ta = new MenuTableAdapter();
    //    ACCDataset.MenuDataTable dt = new ACCDataset.MenuDataTable();

    //    DataSet ds = new DataSet();
    //    ds.DataSetName = "Menus";

    //    ta.MenusTable(dt);
    //    ds.Tables.Add(dt);
    //    ds.Tables[0].TableName = "Menu";

    //    DataRelation relation = new DataRelation("ParentChild",
    //     ds.Tables["Menu"].Columns["MenuID"],
    //     ds.Tables["Menu"].Columns["ParentID"], true);

    //    relation.Nested = true;
    //    ds.Relations.Add(relation);
    //    return ds;


    //}

    private ArrayList _alFirstLevelNames = new ArrayList() { "Manager", "State", "Portfolio", "Market", "Report", "Portfolios", "Region" };
    private string[] _saTableNames = { "Table1", "Table2", "Table3", "Table4", "Table1", "Table3", "Table5" };


    public DataView dvACC_DATA(string psFirstLevelName)
    {

        string sTableName = _saTableNames[_alFirstLevelNames.IndexOf(psFirstLevelName)];

        if ((System.Data.DataSet)HttpContext.Current.Cache["ACC_DATA"] == null) return (new DataTable()).DefaultView;
        DataView dv = ((System.Data.DataSet)HttpContext.Current.Cache["ACC_DATA"]).Tables[sTableName].DefaultView;

        switch (psFirstLevelName)
        {
            case "Manager":
                if (!dv.Table.Columns.Contains("TXT"))
                {
                    dv.Table.Columns.Add("TXT");
                    dv.Table.Columns.Add("VAL");
                    dv.Table.Columns["TXT"].Expression = "IIF(LVL=1,'\xa0\xa0',IIF(LVL=2,'\xa0\xa0\xa0\xa0','')) + ACCT_MGR";
                    dv.Table.Columns["VAL"].Expression = "ACCT_MGR + ':' + LVL";
                }
                return dv;

            case "Market":
                if (!dv.Table.Columns.Contains("TXT"))
                {
                    dv.Table.Columns.Add("TXT");
                    dv.Table.Columns.Add("VAL");
                    dv.Table.Columns["TXT"].Expression = "IIF(LVL=1,'\xa0\xa0',IIF(LVL=2,'\xa0\xa0\xa0\xa0','')) + MKT";
                    dv.Table.Columns["VAL"].Expression = "MARKET  + IIF(LVL<2,':' + LVL,'')";
                }
                return dv;

            case "State":
                if (!dv.Table.Columns.Contains("TXT"))
                {
                    dv.Table.Columns.Add("TXT");
                    dv.Table.Columns.Add("VAL");
                    dv.Table.Columns["TXT"].Expression = "IIF(LVL=1,'\xa0\xa0\xa0',IIF(LVL=2,'\xa0\xa0\xa0\xa0\xa0\xa0','')) + ST";
                    dv.Table.Columns["VAL"].Expression = "STATE  + IIF(LVL<2,':' + LVL,'')";
                }
                return dv;

            case "Report":
                return GetTopReports().DefaultView;

            default:
                return dv;
        }
    }

    public ArrayList CreateRowFilter(string psCookieGroup, bool pbIsRaw)
    {
        string sCookie = String.Empty;
        ArrayList alCookies = new ArrayList(){"Addresses","Greens","Lit","InService","Saturation",
                    "TotalSubscribers","IPTVSubscribers","HSIASubscribers","VOIPSubscribers",
                    "TotalRevenues","IPTVRevenues","HSIARevenues","VOIPRevenues","OtherRevenues",
                    "TotalPercentage","IPTVPercentage","HSIAPercentage","VOIPPercentage","OtherPercentage",
                    "TotalPerSubscriber","IPTVPerSubscriber","HSIAPerSubscriber","VOIPPerSubscriber","OtherPerSubscriber"};

        ArrayList alColumns = new ArrayList(){"Addresses","Greens","Lit","InService","Saturation",
                    "Subscribers","IPTV","HSIA","VOIP",
                    "Total_Revenue","IPTV_Revenue","HSIA_Revenue","VOIP_Revenue","OTHER_UVERSE_Revenue",
                    "","IPTVPercentage","HSIAPercentage","VOIPPercentage","OtherPercentage",
                    "TotalPerSubscriber","IPTVPerSubscriber","HSIAPerSubscriber","VOIPPerSubscriber","OtherPerSubscriber"};

        ArrayList alRowFilters = new ArrayList();

        for (int i = 0; i < alCookies.Count; i++)
        {
            string sCookieName = alCookies[i].ToString();
            string sCookieValue = getCookie(sCookieName, psCookieGroup);

            if (!pbIsRaw) sCookieValue = sCookieValue.Replace("$", "").Replace("%", "").Replace(",", "");
            if (sCookieValue.Contains("&"))
            {
                string[] saCookieValue = sCookieValue.Split('&');
                alRowFilters.Add(alColumns[i] + saCookieValue[0]);
                sCookieValue = saCookieValue[1];
            }

            if (sCookieValue.Length > 0)
            {
                string sFilter = alColumns[i] + sCookieValue;
                if (sFilter.Length > 0) alRowFilters.Add(sFilter);

            }
        }

        return alRowFilters;
    }

    protected string getCookie(string psCookieName, string psCookieGroup)
    {
        string sCookie = String.Format("{0}_{1}", psCookieGroup, psCookieName);
        return HttpContext.Current.Request.Cookies[sCookie] == null ? "" :
         HttpContext.Current.Request.Cookies[sCookie].Value;
    }

    protected DataTable GetTopReports()
    {
        DataTable dt = new DataTable("TopReports");

        DataColumn dc = new DataColumn("TXT", System.Type.GetType("System.String"));
        dt.Columns.Add(dc);
        dc = new DataColumn("VAL", System.Type.GetType("System.String"));
        dt.Columns.Add(dc);
        dt.ReadXml(HttpContext.Current.Server.MapPath("TopReports/reportlist.xml"));
        return dt;
    }

    public string FilterLegend(string sGrouping, string sName)
    {

        ArrayList alRowFilters = (new Menus()).CreateRowFilter(sGrouping, true);
        ArrayList alRowFilterColumnNames = new ArrayList();
        sName = sName.Split(':')[0];
        sName = ConfigurationManager.AppSettings[sName] != null ? ConfigurationManager.AppSettings[sName] : sName;
        for (int i = 0; i < alRowFilters.Count; i++)
        {

            string[] saGreater = alRowFilters[i].ToString().Replace("_", " ").Split(new string[] { ">=" }, StringSplitOptions.None);
            if (saGreater.Length > 1)
            {
                AddColumn(saGreater[0]);
                AddColumn(saGreater[1]);
                alRowFilters[i] = " at least " + saGreater[1] + " " + saGreater[0];
            }

            string[] saLesser = alRowFilters[i].ToString().Replace("_", " ").Split(new string[] { "<=" }, StringSplitOptions.None);
            if (saLesser.Length > 1)
            {
                AddColumn(saLesser[0]);
                AddColumn(saLesser[1]);
                alRowFilters[i] = " no more than " + saLesser[1] + " " + saLesser[0];
            }

            saGreater = alRowFilters[i].ToString().Replace("_", " ").Split('>');
            if (saGreater.Length > 1)
            {
                AddColumn(saGreater[0]);
                AddColumn(saGreater[1]);
                alRowFilters[i] = " over " + saGreater[1] + " " + saGreater[0];
            }

            saLesser = alRowFilters[i].ToString().Replace("_", " ").Split('<');
            if (saLesser.Length > 1)
            {
                AddColumn(saLesser[0]);
                AddColumn(saLesser[1]);
                alRowFilters[i] = " under " + saLesser[1] + " " + saLesser[0];
            }
        }

        if (alRowFilters.Count > 0)
        {
            RowFilter = " with " + string.Join(" and ", (alRowFilters.ToArray(typeof(string)) as string[])).Replace(">", " Over ").Replace("<", " Under ");
        }

        return sGrouping + " " + RowFilter + (string)((sName.Length == 0) ? "" : "—" + sName);
    }


    private String _RowFilter = String.Empty;
    public String RowFilter
    {
        get { return _RowFilter; }
        set { _RowFilter = value; }
    }

    private ArrayList alRowFilterColumnNames;

    private void AddColumn(string psColumnName)
    {
        if (alRowFilterColumnNames == null) alRowFilterColumnNames = new ArrayList();
        if (psColumnName == null) return;
        if (Char.IsNumber(psColumnName, 0)) return;
        if (alRowFilterColumnNames.Contains(psColumnName)) return;
        alRowFilterColumnNames.Add(psColumnName);
        HttpContext.Current.Session["alRowFilterColumnNames"] = alRowFilterColumnNames;
    }
}